package com.itwh.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itwh.project.common.DataGridView;
import com.itwh.project.common.ResultObj;
import com.itwh.project.domain.Course;
import com.itwh.project.domain.Department;
import com.itwh.project.service.CourseService;
import com.itwh.project.service.DepartmentService;
import com.itwh.project.service.LabLaboratoryCourseService;
import com.itwh.project.vo.CourseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 翁煌
 * @since 2020-07-05
 */
@Api(value = "实验室可做课程")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private LabLaboratoryCourseService labLaboratoryCourseService;

    @Autowired
    private DepartmentService departmentService;


    @ApiOperation(value = "课程显示")
    @GetMapping("/laboratory_course/{lab_id}")
    public DataGridView list(@PathVariable("lab_id") Long lab_id , CourseVo courseVo){
        //查出编号为lab_id的实验室有哪些课程（courseIds）可以上
        List<Long> courseIds = labLaboratoryCourseService.findCourseIdByLabId(lab_id);

        //根据课程id去查找对应的课程
         return courseService.listCourse(courseIds,courseVo);
    }

    @ApiOperation(value = "添加可做课程")
    @PostMapping("/laboratory_course/{lab_id}")
    public ResultObj addCourse(@PathVariable("lab_id")Long lab_id,CourseVo courseVo){
        Long cid = courseVo.getId();
        //保存这个课程id和这间实验室的关系
        return labLaboratoryCourseService.addCourseId(courseVo.getCollegeId(),lab_id, cid,courseVo.getByWho(),courseVo.getDeleted(),courseVo.getLastVersion());

    }

    @ApiOperation(value = "删除可做课程")
    @DeleteMapping("/laboratory_course/{lab_id}/{cid}")
    public ResultObj deleteCourse(@PathVariable("lab_id")Long lab_id,@PathVariable("cid")Long cid){
        //删除实验室-课程的关系
        return labLaboratoryCourseService.updateDeletedByLabIdAndCId(lab_id,cid);
    }

    //因为课程信息是其他组的，所以不能修改课程的信息
//    @ApiOperation(value = "修改可做课程")
//    @PutMapping("/laboratory_course")
//    public ResultObj updateCourse(CourseVo courseVo){
//        return courseService.updateCourse(courseVo);
//    }

//    @ApiOperation(value = "加载下一个要添加的课程id")
//    @GetMapping("loadcourseId")
//    public Map<String,Object> loadDeptMaxOrderNum(){
//        Map<String,Object> map=new HashMap<>();
//        QueryWrapper<Course> queryWrapper=new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//        //按照排序码降序查询所有课程
//        List<Course> list = this.courseService.list(queryWrapper);
//        if(list.size()>0){
//            //获取第一个，就是排序码最大值再加1
//            map.put("value",list.get(0).getId()+1);
//        }else {
//            map.put("value",1000);
//        }
//        return map;
//    }
}

